#!/bin/bash

PORT_NAME=$1
IFINDEX=$2

PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin

is_bf=`lspci -s 00:00.0 2> /dev/null | grep -wq "PCI bridge: Mellanox Technologies" && echo 1 || echo 0`
if [ $is_bf -ne 1 ]; then
	exit 0
fi

for rep_ndev in `ls /sys/class/net/`; do
	_ifindex=`cat /sys/class/net/$rep_ndev/ifindex | head -1 2>/dev/null`
	if [ "$_ifindex" = "$IFINDEX" ]
	then
		devpath=`udevadm info /sys/class/net/$rep_ndev | grep "DEVPATH="`
		pcipath=`echo $devpath | awk -F "/net/$rep_ndev" '{print $1}'`
		array=($(echo "$pcipath" | sed 's/\// /g'))
		len=${#array[@]}
		# last element in array is pci parent device
		parent_pdev=${array[$len-1]}
		#pdev is : 0000:03:00.0, so extract them by their index
		b=`echo ${parent_pdev:5:2} | sed 's/^0//'`
		f=${parent_pdev: -1}
		echo "NAME=en${b}f${f}${PORT_NAME}"
		exit
	fi
done
